// pages/myself/create-feedback/createFeedback.js
const app = getApp()
const { baseUrl } = require('../../../utils/baseUrl.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    treeList: [],
    secondTreeList: [],
    form: {
      description: '',
      firstClassificationIdList: [],
      secondClassificationIdList: [],
      imgResourceIdList: [],
    },
    imageList: [],
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFeedbackTree()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  closeImage (e) {
    this.data.imageList.splice(e.currentTarget.id, 1)
    this.setData({
      imageList: this.data.imageList
    })
  },
  updataImage () {
    var that = this
    wx.chooseImage({
      count: 5-that.data.imageList.length || 5, // 默认9 
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有  
      success: function (res) {
        let formData = {
          method: 'upload_multipart_file',
          app_key: null,
          session: wx.getStorageSync("token"),
          v: '1.0'
        }
          var tempFilePaths = res.tempFilePaths
          wx.showLoading({
            title: '上传中',
            mask: true
          })
          for (var i = 0; i < tempFilePaths.length; i++) {
              wx.uploadFile({
                url: `https://${baseUrl}/api/cpic/uploadResource`,
                  filePath: tempFilePaths[i],
                  name: 'file',
                  header: {
                    accept: 'application/json'
                  },
                  formData,
                  success: function (res) {
                    let data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
                    that.data.imageList.push(data.data)
                    that.setData({
                      imageList: that.data.imageList
                    })
                    if (i === tempFilePaths.length) {
                      wx.hideLoading()
                    }
                  }
              })
          }
        }
    })
  },
  bindKeyInput: function (e) {
    var value = e.detail.value
    console.log(e, e.detail.value, e.detail.value.length)
    if (value.length > 200) {
      value = value.substr(0, 200)
    }
    console.log(value, value.length)
     this.setData({
        'form.description': value
     })
  },
  choice (e) {
    let item = e.currentTarget.dataset.item
    let arr = []
    this.data.treeList.forEach((v, k) => {
      if (item.id === v.id) {
        if (v.cur) {
          v.cur = false
            var c1 = this.data.secondTreeList.filter(a=>{
            return ! v.feedbackClassificationTrees.some(c=>(c.id === a.id))
            })
            this.setData({
              secondTreeList: c1
            })
        } else {
          v.cur = true
          arr = v.feedbackClassificationTrees
        }
      }
    })
    this.setData({
      treeList: this.data.treeList,
      secondTreeList: this.data.secondTreeList.concat(arr)
    })
  },
  select (e) {
    let item = e.currentTarget.dataset.item
    this.data.secondTreeList.forEach((v, k) => {
      if (item.id === v.id) {
        if (v.scur) {
          v.scur = false
        } else {
          v.scur = true
        }
      }
    })
    this.setData({
      secondTreeList: this.data.secondTreeList
    })
  },
  getFeedbackTree () {
    app.api.post({
      url: 'feedbackClassification_tree',
      data: {id: 0}
    }).then(res => {
      let arr = []
      let arr2 = []
      if (res.length > 0) {
        arr = res || []
        arr.forEach((v, k) => {
          v.cur = false
        })
        arr[0].cur = true
        if (arr[0].feedbackClassificationTrees.length > 0) {
          arr2 = arr[0].feedbackClassificationTrees
        }
        arr2.forEach((v, k) => {
          v.scur = false
        })
        this.setData({
          treeList: arr,
          secondTreeList: arr2
        })
      }
    })
  },
  submit: function () {
    let treeListIds = []
    let secondTreeListIds = []
    let resourceId = []
    this.data.treeList.forEach((v) => {
      if (v.cur){
        treeListIds.push(v.id)
      }
    })
    this.data.secondTreeList.forEach((v) => {
      if (v.scur) {
        secondTreeListIds.push(v.id)
      }
    })
    this.data.imageList.forEach((v) => {
      resourceId.push(v.resourceId)
    })
    if (treeListIds.length === 0 && secondTreeListIds.length === 0) {
      wx.showToast({
        title: '请选择问题分类',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.data.form.description) {
      wx.showToast({
        title: '请填写描述',
        icon: 'none',
        duration: 2000
      })
      return
    }
    this.data.form.firstClassificationIdList = treeListIds
    this.data.form.secondClassificationIdList = secondTreeListIds
    this.data.form.imgResourceIdList = resourceId
    this.setData({
      loading: true,
    })
    app.api.post({
      url: 'vx_feedback_createFeedback',
      data: this.data.form
    }).then(res =>{
      wx.showToast({
        title: '提交成功',
        icon: 'none',
        duration: 500
      })
      setTimeout(() => {
        wx.navigateBack({delta: 1})
        this.setData({
          loading: false
        })
      }, 500)
    }, () => {
      this.setData({
        loading: false
      })
    })
  },
})